package com.jlf.controller.engine;

import com.jlf.core.annotation.restful.controller.RestfulController;
import com.jlf.core.annotation.restful.method.RestfulDelete;
import com.jlf.core.annotation.restful.method.RestfulPost;
import com.jlf.core.engine.domain.SchemaCriterionDO;
import com.jlf.core.engine.domain.SchemaDomainDO;
import com.jlf.core.engine.domain.SchemaFieldDO;
import com.jlf.core.engine.domain.SchemaIndexDO;
import com.jlf.core.engine.persistence.SchemaCriterionPersistence;
import com.jlf.core.engine.persistence.SchemaDomainPersistence;
import com.jlf.core.engine.persistence.SchemaFieldPersistence;
import com.jlf.core.engine.persistence.SchemaIndexPersistence;
import com.jlf.core.engine.driver.SchemaDriveServer;
import com.jlf.core.error.exception.NoObjectException;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * 引擎的接口控制器
 *
 * @author wujr
 * 2023/7/8
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/8 1.0 新增]
 */
@Slf4j
@Tag(name = "模型领域引擎管理接口")
@RestfulController(path = "/rest/jlf/core/engine/schema/drive", name = "模型领域引擎管理接口")
@RequiredArgsConstructor
public class SchemaDriveController {
    private final SchemaDomainPersistence schemaDomainPersistence;
    private final SchemaFieldPersistence schemaFieldPersistence;
    private final SchemaIndexPersistence schemaIndexPersistence;
    private final SchemaCriterionPersistence schemaCriterionPersistence;
    private final SchemaDriveServer schemaDriveServer;

    @RestfulPost(code = "publishSchemaDomainDriver", name = "发布模型领域驱动", path = "/{domainId}")
    public void publishSchemaDomainDriver(@PathVariable("domainId") Long domainId) throws Exception{
        SchemaDomainDO schemaDomain = schemaDomainPersistence.getById(domainId);
        if (schemaDomain == null){
            log.info("发布模型领域驱动：{}，该模型领域不存在", domainId);
            throw new NoObjectException(SchemaDomainDO.class, "模型领域不存在");
        }
        List<SchemaFieldDO> schemaFields = schemaFieldPersistence.loadByDomain(schemaDomain.getDomainCode(), null);
        List<SchemaIndexDO> schemaIndexes = schemaIndexPersistence.loadByDomain(schemaDomain.getDomainCode());
        List<SchemaCriterionDO> schemaCriteria = schemaCriterionPersistence.loadByDomain(schemaDomain.getDomainCode());
        schemaDriveServer.publishDomain(schemaDomain, schemaFields, schemaIndexes, schemaCriteria,true);
    }

    @RestfulDelete(code = "withdrawSchemaDomainDriver", name = "撤销模型领域驱动", path = "/{domainId}")
    public void withdrawSchemaDomainDriver(@PathVariable("domainId") Long domainId) throws Exception{
        SchemaDomainDO schemaDomain = schemaDomainPersistence.getById(domainId);
        if (schemaDomain == null){
            log.info("撤销模型领域驱动：{}，该模型领域不存在", domainId);
            throw new NoObjectException(SchemaDomainDO.class, "模型领域不存在");
        }
        schemaDriveServer.withdrawDomain(schemaDomain);
    }
}
